<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author PR
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button
          type="default"
          @click="handleExport()"
          v-auth="'wh:sh:whTmSh:edit'"
          v-if="props.status != '3'"
        >
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <a-button
          type="default"
          @click="handleImport()"
          v-auth="'wh:sh:whTmSh:edit'"
          v-if="props.status != '3'"
        >
          <Icon icon="ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
        <a-button
          type="primary"
          @click="handleForm({})"
          v-auth="'wh:sh:whTmSh:edit'"
          v-if="props.status != '3'"
        >
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
        <a-button type="default" @click="handlePrint({})" v-auth="'wh:sh:whTmSh:edit'">
          <Icon icon="ant-design:printer-outlined" /> {{ t('打印') }}
        </a-button>
        <Popconfirm placement="bottom" :title="t('是否确认批量审核？')" @confirm="handleData">
          <a-button type="default" v-auth="'wh:sh:whTmSh:sh'" v-if="props.status == '3'">
            <Icon icon="ant-design:check-outlined" /> {{ t('审核') }}
          </a-button>
        </Popconfirm>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })" v-if="record.status != '2'">
          {{ record.cinvcode }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
    <ListPos @register="registerPosDrawer" @success="handleSuccess" />
    <PrintModal @register="registerPrintModal" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsWhShWhTmShList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { whTmShDelete, whTmShListData,whTmShHandleToU8 } from '/@/api/wh/sh/whTmSh';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';
  import ListPos from './listPos.vue';
  import PrintModal from '/@/components/print/printModal.vue';
  import { propTypes } from '/@/utils/propTypes';
  import { Popconfirm } from 'ant-design-vue';
  
  const { t } = useI18n('wh.sh.whTmSh');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('收料管理'),
  };
  console.log(router);
  const props = defineProps({
    status: propTypes.string,
  });
  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('存货代码'),
        field: 'basInv.cinvaddcode',
        component: 'Input',
      },
      {
        label: t('存货编码'),
        field: 'cinvcode',
        component: 'Input',
      },
      {
        label: t('存货名称'),
        field: 'basInv.cinvname',
        component: 'Input',
      },
      {
        label: t('批次'),
        field: 'cbatch',
        component: 'Input',
      },
      {
        label: t('仓库'),
        field: 'cwhcode',
        component: 'Input',
      },
      {
        label: t('货位'),
        field: 'cposcode',
        component: 'Input',
      },
      {
        label: t('箱号'),
        field: 'boxCode',
        component: 'Input',
      },
      {
        label: t('操作者'),
        field: 'cpersoncode',
        component: 'Input',
      },
      {
        label: t('供应商'),
        field: 'cvencode',
        component: 'Input',
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'ASD_STATUS ',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('备注'),
        field: 'remarks',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 80,
      align: 'center',
      dictType: 'ASD_STATUS ',
    },
    {
      title: t('存货编码'),
      dataIndex: 'cinvcode',
      key: 'a.cinvcode',
      sorter: true,
      width: 150,
      align: 'left',
      // status 为 2 时，不加插槽
      slot: 'firstColumn',
    },
    {
      title: t('存货代码'),
      dataIndex: 'basInv.cinvaddcode',
      key: 'inv.cinvaddcode',
      sorter: true,
      width: 130,
      align: 'left',
      // defaultHidden: true,
    },
    {
      title: t('存货名称'),
      dataIndex: 'basInv.cinvname',
      key: 'inv.cinvname',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('规格型号'),
      dataIndex: 'basInv.cinvstd',
      key: 'inv.cinvstd',
      sorter: true,
      width: 130,
      align: 'left',
      defaultHidden: true,
    },
    {
      title: t('批次'),
      dataIndex: 'cbatch',
      key: 'a.cbatch',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('仓库'),
      dataIndex: 'cwhcode',
      key: 'a.cwhcode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('数量'),
      dataIndex: 'iqty',
      key: 'a.iqty',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('箱号'),
      dataIndex: 'boxCode',
      key: 'a.boxCode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('操作者'),
      dataIndex: 'cpersoncode',
      key: 'a.cpersoncode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('供应商'),
      dataIndex: 'cvencode',
      key: 'a.cvencode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('货位'),
      dataIndex: 'cposcode',
      key: 'a.cposcode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('创建日期'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        label: t('指定货位'),
        onClick: handlePos.bind(this, { cwhcode: record.cwhcode, id: record.id }),
        auth: 'wh:sh:whTmSh:posAdd',
        ifShow: record.status != '2',
      },
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑收料'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'wh:sh:whTmSh:edit',
        ifShow: record.status != '2',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除收料'),
        popConfirm: {
          title: t('是否确认删除收料'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'wh:sh:whTmSh:edit',
        ifShow: record.status != '2',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerPrintModal, { openModal: openPrintModal }] = useModal();
  const [registerPosDrawer, { openDrawer: openPosDrawer }] = useDrawer();

  const [registerTable, { reload, getSelectRows, setProps }] = useTable({
    api: whTmShListData,
    beforeFetch: (params) => {
      if (props.status != '') {
        params.status = props.status;
      }
      return params;
    },
    columns: tableColumns,
    // actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  onMounted(() => {
    if (props.status != '3') {
      setProps({
        actionColumn: actionColumn,
      });
    }
    setProps({
      rowSelection: {
        type: 'checkbox',
      },
    });
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function handlePos(record: Recordable) {
    openPosDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/wh/sh/whTmSh/exportData',
      target: '_self',
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleDelete(record: Recordable) {
    const res = await whTmShDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    console.log('handleSuccess');
    reload();
  }

  async function handlePrint() {
    let arr = await getSelectRows();
    if (getSelectRows().length == 0) {
      showMessage(t('请先选择一行数据'));
      return;
    }
    const idsArr = arr.map((item) => {
      return item.id;
    });
    let params = {
      ids: idsArr.join(','),
      title: '标识卡打印',
      height: '0.9',
      width: '0.9',
      fileName: '标识卡',
    };
    openPrintModal(true, params);
  }

  async function handleData(){
    try {
      let arr = await getSelectRows();
      if (getSelectRows().length == 0) {
        showMessage(t('请先选择一行数据'));
        return;
      }
      const idsArr = arr.map((item) => {
        return item.id;
      });
      let params = {
        selIds: idsArr.join(','),
      };
      const res = await whTmShHandleToU8(params);
      showMessage(res.message);
      handleSuccess();
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }

</script>
